package com.mm.cloth.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.mm.cloth.R;
import com.mm.cloth.model.Match;

import java.util.List;

public class MatchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int VIEW_TYPE_ITEM = 0;
    public static final int VIEW_TYPE_LOADING = 1;

    private Context context;
    private List<Match> matchList;
    private OnMatchClickListener onMatchClickListener;
    private boolean isLoadingAdded = false;

    public interface OnMatchClickListener {
        void onMatchClick(Match match);
    }

    public MatchAdapter(Context context, List<Match> matchList) {
        this.context = context;
        this.matchList = matchList;
    }

    public void setOnMatchClickListener(OnMatchClickListener listener) {
        this.onMatchClickListener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_match, parent, false);
            return new MatchViewHolder(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.item_loading_footer, parent, false);
            return new LoadingViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MatchViewHolder) {
            bindMatchViewHolder((MatchViewHolder) holder, position);
        }
        // LoadingViewHolder不需要绑定数据
    }

    private void bindMatchViewHolder(MatchViewHolder holder, int position) {
        Match match = matchList.get(position);
        
        holder.tvTitle.setText(match.getTitle());
        holder.tvCategory.setText(match.getCategoryName());
        
        // 设置点赞数和浏览量
        holder.tvLikes.setText(String.valueOf(match.getLikes()));
        holder.tvViews.setText(String.valueOf(match.getViews()));
        
        // 加载图片
        if (match.getImageUrl() != null && !match.getImageUrl().isEmpty()) {
            Glide.with(context)
                    .load(match.getImageUrl())
                    .placeholder(R.drawable.placeholder_clothes)
                    .error(R.drawable.placeholder_clothes)
                    .centerCrop()
                    .into(holder.ivMatch);
        } else {
            holder.ivMatch.setImageResource(R.drawable.placeholder_clothes);
        }
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (onMatchClickListener != null) {
                onMatchClickListener.onMatchClick(match);
            }
        });
    }

    @Override
    public int getItemCount() {
        return matchList != null ? matchList.size() + (isLoadingAdded ? 1 : 0) : 0;
    }

    @Override
    public int getItemViewType(int position) {
        return (position == matchList.size() && isLoadingAdded) ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
    }

    public void addLoadingFooter() {
        isLoadingAdded = true;
        notifyItemInserted(matchList.size());
    }

    public void removeLoadingFooter() {
        if (isLoadingAdded) {
            isLoadingAdded = false;
            int position = matchList.size();
            notifyItemRemoved(position);
        }
    }

    public static class MatchViewHolder extends RecyclerView.ViewHolder {
        ImageView ivMatch;
        TextView tvTitle;
        TextView tvCategory;
        TextView tvLikes;
        TextView tvViews;

        public MatchViewHolder(@NonNull View itemView) {
            super(itemView);
            ivMatch = itemView.findViewById(R.id.ivMatch);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvCategory = itemView.findViewById(R.id.tvCategory);
            tvLikes = itemView.findViewById(R.id.tvLikes);
            tvViews = itemView.findViewById(R.id.tvViews);
        }
    }
    
    public static class LoadingViewHolder extends RecyclerView.ViewHolder {
        public LoadingViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
} 